package com.ruoyi.mobile.project.pms.service;

import com.github.pagehelper.PageHelper;
import com.ruoyi.db.pms.domain.PmsGoods;
import com.ruoyi.db.pms.domain.query.PmsGoodsQueryVo;
import com.ruoyi.db.pms.mapper.PmsGoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class WxPmsGoodsService
{
    @Autowired
    private PmsGoodsMapper goodsMapper;

    /**
     * 获取热卖商品
     *
     * @param offset
     * @param limit
     * @return
     */
    public List<PmsGoods> queryByHot(int offset, int limit)
    {
        PmsGoodsQueryVo goodsQueryVo = new PmsGoodsQueryVo();
        goodsQueryVo.setIsOnSale(true);
        goodsQueryVo.setIsHot(true);
        PageHelper.startPage(offset, limit);
        return goodsMapper.selectPmsGoodsList(goodsQueryVo);
    }

    /**
     * 获取新品上市
     *
     * @param offset
     * @param limit
     * @return
     */
    public List<PmsGoods> queryByNew(int offset, int limit)
    {
        PmsGoodsQueryVo goodsQueryVo = new PmsGoodsQueryVo();
        goodsQueryVo.setIsOnSale(true);
        goodsQueryVo.setIsNew(true);

        PageHelper.startPage(offset, limit);
        return goodsMapper.selectPmsGoodsList(goodsQueryVo);
    }

    /**
     * 获取分类下的商品
     *
     * @param catList
     * @param offset
     * @param limit
     * @return
     */
    public List<PmsGoods> queryByCategory(List<Long> catList, int offset, int limit)
    {
        return null;
    }

    /**
     * 获取一级分类下的二级分类的所商品
     *
     * @param catL1Id
     * @param offset
     * @param limit
     * @return
     */
    public List<PmsGoods> queryGoodsByCategory(Long catL1Id, int offset, int limit)
    {
        PageHelper.startPage(offset, limit);
        return goodsMapper.selectGoodsInLevel2Category(catL1Id);
    }

    /**
     * 获取分类下的商品
     *
     * @author 张涛
     * @param catId
     * @param offset
     * @param limit
     * @return
     */
    public List<PmsGoods> queryByCategory(Long catId, int offset, int limit)
    {
        PmsGoodsQueryVo pmsGoodsQueryVo = new PmsGoodsQueryVo();
        pmsGoodsQueryVo.setCategoryId(catId);
        pmsGoodsQueryVo.setIsOnSale(true);
        PageHelper.startPage(offset, limit);
        return goodsMapper.selectPmsGoodsList(pmsGoodsQueryVo);
    }

    /**
     * 查询二级类目下的商品
     *
     * @author 张涛
     * @param catId
     * @param brandId
     * @param keywords
     * @param isHot
     * @param isNew
     * @param offset
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    public List<PmsGoods> querySelective(Long catId, Long brandId, String keywords, Boolean isHot, Boolean isNew, Integer offset, Integer limit, String sort, String order)
    {
        PmsGoodsQueryVo pmsGoodsQueryVo = new PmsGoodsQueryVo();
        if (!StringUtils.isEmpty(catId) && catId != 0) {
            pmsGoodsQueryVo.setCategoryId(catId);
        }
        if (!StringUtils.isEmpty(brandId)) {
            pmsGoodsQueryVo.setBrandId(brandId);
        }
        if (!StringUtils.isEmpty(isNew)) {
            pmsGoodsQueryVo.setIsNew(isNew);
        }
        if (!StringUtils.isEmpty(isHot)) {
            pmsGoodsQueryVo.setIsHot(isHot);
        }
        if (!StringUtils.isEmpty(keywords)) {
            pmsGoodsQueryVo.setName( keywords);
        }

        pmsGoodsQueryVo.setIsOnSale(true);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            pmsGoodsQueryVo.setSort(sort);
            pmsGoodsQueryVo.setOrder(order);
        }
        PageHelper.startPage(offset, limit);
        return goodsMapper.selectPmsGoodsL2List(pmsGoodsQueryVo);
    }

    public List<PmsGoods> querySelective(Long goodsId, String goodsSn, String name, Integer page, Integer size, String sort, String order)
    {
        return null;
    }

    /**
     * 获取某个商品信息,包含完整信息
     *
     * @param id
     * @return
     */
    public PmsGoods findById(Long id)
    {
        return goodsMapper.selectPmsGoodsById(id);
    }

    /**
     * 获取某个商品信息，仅展示相关内容
     *
     * @param id
     * @return
     */
    public PmsGoods findByIdVO(Long id)
    {
        return null;
    }

    /**
     * 获取所有在售物品总数
     *
     * @author 张涛
     * @return
     */
    public Integer queryOnSale()
    {
        PmsGoodsQueryVo pmsGoodsQueryVo = new PmsGoodsQueryVo();
        pmsGoodsQueryVo.setIsOnSale(true);
        return goodsMapper.countOnSale(pmsGoodsQueryVo);
    }

    /**
     * 修改商品信息
     *
     * @author 张涛
     * @param goods
     * @return
     */
    public int updateById(PmsGoods goods)
    {
        return goodsMapper.updatePmsGoods(goods);
    }

    /**
     * 根据id删除商品信息
     *
     * @author 张涛
     * @param id
     */
    public void deleteById(Long id)
    {
        goodsMapper.deletePmsGoodsById(id);
    }

    /**
     * 添加商品
     *
     * @author 张涛
     * @param goods
     */
    public void add(PmsGoods goods)
    {
        goodsMapper.insertPmsGoods(goods);
    }

    /**
     * 获取所有物品总数，包括在售的和下架的，但是不包括已删除的商品
     *
     * @return
     */
    public int count()
    {
        return 0;
    }

    /**
     * 毛思淼
     * 获取商品的类目id
     *
     * @return
     */

    public List<Long> getCatIds(Long brandId, String keywords, Boolean isHot, Boolean isNew)
    {

        PmsGoodsQueryVo pmsGoodsQueryVo = new PmsGoodsQueryVo();
        if(!StringUtils.isEmpty(brandId))
        {
            pmsGoodsQueryVo.setBrandId(brandId);
        }
        if(!StringUtils.isEmpty(isHot))
        {
            pmsGoodsQueryVo.setIsHot(isHot);
        }
        if(!StringUtils.isEmpty(isNew))
        {
            pmsGoodsQueryVo.setIsNew(isNew);
        }
        if(!StringUtils.isEmpty(keywords))
        {
            pmsGoodsQueryVo.setName(keywords);
        }
        pmsGoodsQueryVo.setIsOnSale(true);
        List<PmsGoods> goodsList = goodsMapper.selectPmsGoodsCateIdList(pmsGoodsQueryVo);
        //将数据存储在list里面
        List<Long> cats = new ArrayList<Long>();
        for (PmsGoods goods : goodsList) {
            cats.add(goods.getCategoryId());
        }

        return cats;
    }

    public boolean checkExistByName(String name)
    {
        return true;
    }

    public List<PmsGoods> queryByIds(Integer[] ids)
    {
        return null;
    }
}
